﻿namespace HomeCloudStorageSystem.Helps
{
    /// <summary>
    /// 自定义类型转换工具
    /// </summary>
    public static class ReadStreamHelp
    {
        /// <summary>
        /// Stream 类型转 byte[] 类型
        /// </summary>
        /// <param name="stream">Stream流参数</param>
        /// <returns>byte[]</returns>
        public static byte[] ReadStream(Stream stream)
        {
            var buffer = new byte[stream.Length];
            var bytesRead = stream.Read(buffer, 0, buffer.Length);
            var fileChunk = new byte[bytesRead];
            Array.Copy(buffer, fileChunk, bytesRead);
            return fileChunk;
        }

        public static string GetContentType(string filename)
        {
            var types = GetMimeTypes();
            var ext = Path.GetExtension(filename).ToLowerInvariant();
            return types[ext];
        }
        /// <summary>
        /// 根据扩展名，返回 content-Type
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetMimeTypes()
        {
            return new Dictionary<string, string>
            {
                {".txt", "text/plain"},
                {".pdf", "application/pdf"},
                {".doc", "application/vnd.ms-word"},
                {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
                {".xls", "application/vnd.ms-excel"},
                {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
                {".png", "image/png"},
                {".jpg", "image/jpeg"},
                {".jpeg", "image/jpeg"},
                {".gif", "image/gif"},
                {".bmp", "image/bmp"},
                {".mp4","video/mp4" },
                {".mp3","audio/mpeg" },
                {".wav","audio/x-wav" },
                {".avi","video/x-msvideo" },
                {".mov","video/quicktime" },
                {".flv","video/x-flv" },
                {".mkv","video/x-matroska" },
                {".webp","image/webp" },
                {".tiff","image/tiff" },
                {".zip","application/zip" },
                {".rar","application/x-rar-compressed" },
                {".ppt","application/vnd.ms-powerpoint" },
                {".pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation" },
                {".xml","application/xml" },
                {".json","application/json" },
                {".js","application/javascript" },
                {".css","text/css" },
                {".html","text/html" },
                {".sql","application/sql" },
                {".exe","application/octet-stream" },
                {".dm","application/octet-stream" },
            };
        }
    }
}
